package com.veryedu.hibernate.action;

import java.util.List;

import com.veryedu.hibernate.biz.DPBiz;
import com.veryedu.hibernate.biz.bean.FilmTimeBean;
import com.veryedu.hibernate.biz.bean.SeatsBean;
import com.veryedu.hibernate.biz.bean.TicketBean;
import com.veryedu.hibernate.exception.BizzException;
import com.veryedu.hibernate.hbm.dao.FilmDao;
import com.veryedu.hibernate.hbm.dao.impl.FilmDaoImpl;
import com.veryedu.hibernate.hbm.entity.Film;
import com.veryedu.hibernate.hbm.entity.Order;

public class DPAction extends BaseAction{
	private List<Film> filmList;
	private Integer filmid;
	private Integer filmId;
	private Integer showTimeId;
	private List<Integer> buySeats;
	private Float price;
	private Float totalPrice;
	private String userName;
	private String password;
	
	private DPBiz dpBiz;
	
	public DPBiz getDpBiz() {
		return dpBiz;
	}

	public void setDpBiz(DPBiz dpBiz) {
		this.dpBiz = dpBiz;
	}

	public Integer getFilmId() {
		return filmId;
	}

	public void setFilmId(Integer filmId) {
		this.filmId = filmId;
	}

	public Integer getShowTimeId() {
		return showTimeId;
	}

	public void setShowTimeId(Integer showTimeId) {
		this.showTimeId = showTimeId;
	}

	public List<Film> getFilmList() {
		return filmList;
	}

	public void setFilmList(List<Film> filmList) {
		this.filmList = filmList;
	}
	
	public Integer getFilmid() {
		return filmid;
	}

	public void setFilmid(Integer filmid) {
		this.filmid = filmid;
	}
	
	public List<Integer> getBuySeats() {
		return buySeats;
	}

	public void setBuySeats(List<Integer> buySeats) {
		this.buySeats = buySeats;
	}
	
	public Float getPrice() {
		return price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}

	public Float getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(Float totalPrice) {
		this.totalPrice = totalPrice;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String filmList(){
		System.out.println("filmList()");
		FilmDao filmDao = new FilmDaoImpl();
		filmList = filmDao.findAll(Film.class);
		this.setRequestAttribute("filmList", filmList);
		return SUCCESS;
	}
	
	public String timeList(){
		System.out.println("timeList()");
		FilmTimeBean filmTime = dpBiz.filmTime(filmid);
		this.setRequestAttribute("filmTime", filmTime);
		return SUCCESS;
	}
	
	public String seatList(){
		System.out.println("seatList()");
		SeatsBean seats = dpBiz.seatList(showTimeId);
		System.out.println(seats.getFilmId());
		this.setRequestAttribute("seats", seats);
		return SUCCESS;
	}
	
	public String viewTicket(){
		List<TicketBean> tickets = dpBiz.viewTicket(filmId,showTimeId,price,buySeats);
		this.setSessionAttribute("tickets", tickets);
		this.setSessionAttribute("filmId", filmId);
		this.setSessionAttribute("showTimeId", showTimeId);
		this.setSessionAttribute("price", price);
		Float totalPrice = price * buySeats.size();
		this.setSessionAttribute("totalPrice", totalPrice);
		return SUCCESS;
	}
	
	public String createOrder(){
		try{
			Order order = dpBiz.createOrder(userName,password,showTimeId,price,totalPrice,buySeats);
			this.setRequestAttribute("order", order);
		}catch(BizzException bizzExcp){
			this.clearErrorsAndMessages();
			this.addActionError(bizzExcp.getErrorMsg());
			return INPUT;
		}
		return SUCCESS;
	}
}
